<!--  -->
<template>
  <div class='tab-box'>
    <!--工具条 查询-->

    <div>

      <el-form :inline="true"
               >
                <el-form-item>
        <el-button type="primary"
                     @click="goback">重置搜索</el-button>
        </el-form-item>
        <el-form-item >
          <el-input v-model="search"
                    placeholder="请输入搜索内容"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     @click="Search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="handleAdd">新增</el-button>
          <el-button type="primary"
                     @click="export_Xls">导出</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--列表-->
    <el-table ref="multipleTable"
              :data="outinfo.slice((currentPage-1)*pageSize,currentPage*pageSize)"
              tooltip-effect="dark"
              style="width: 100%"
              height="400px"
              v-loading="loading"
              :header-cell-style="{'text-align':'center'}"
              :cell-style="{'text-align':'center'}">

      <el-table-column fixed
                       type="selection"
                       width="55">
      </el-table-column>

      <el-table-column prop="robot_part"
                       label="入库零件"
                       width="120">
      </el-table-column>

      <el-table-column prop="part_in_num"
                       label="入库数量"
                       width="120">
      </el-table-column>

      <el-table-column prop="part_in_time"
                       label="入库时间"
                       width="180">
      </el-table-column>
      <el-table-column prop="others"
                       label="其他信息"
                       width="300">
      </el-table-column>
      <el-table-column label="操作"
                       width="150"
                       fixed="right">
        <template slot-scope="scope">
          <el-button size="small"
                     @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
          <!-- <el-button size="small"
                     type="danger"
                     @click="handleDel(scope.$index,scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!--工具条 批量删除-->
    <div class="toolbar">
      <!-- <el-button type="danger"
                 
                 :disabled="this.sels.length===0">批量删除</el-button> -->
      <el-pagination 
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    
                    :current-page="currentPage"
                    :page-sizes="[10,20,50]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="outinfo.length">
      </el-pagination>
    </div>

    <!--编辑新增 界面-->
    <el-dialog :title="textMap[dialogStatus]"
               :visible.sync="dialogVisible">
      <el-form :model="editForm"
               label-width="80px"
               ref="editForm"
               :rules="editFormRules">
        <el-form-item label="入库零件"
                      prop="robot_part">
          <el-input v-model="editForm.robot_part"
                    auto-complete="off"></el-input>
        </el-form-item>
          <el-form-item label="入库数量"
                      prop="part_in_num">
          <el-input type="int" v-model="editForm.part_in_num"
                    auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="入库时间">
          <el-date-picker type="date"
                          placeholder="选择日期"
                          v-model="editForm.part_in_time"></el-date-picker>
        </el-form-item>
        <el-form-item label="其他信息">
          <el-input type="textarea"
                    v-model="editForm.others"></el-input>
        </el-form-item>
      </el-form>
      
      <span slot="footer"
            class="dialog-footer">
        <el-button size="small"
                   @click.native="dialogVisible = false">取 消</el-button>
        <el-button size="small"
                   v-if="dialogStatus=='create'"
                   type="primary"
                   @click="createData">添 加</el-button>
        <el-button size="small"
                   v-else
                   type="primary"
                   @click="updateData">修 改</el-button>
      </span>
    </el-dialog>

  </div>



</template>

<script>

import { Ininfo,Addinfo,Updtininfo } from "../../api/api.js";
import date from '../../utils/date.js';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data () {
    //这里存放数据
    return {

      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 10, // 每页的数据条数
      search:'',//搜索数据
      loading: false,
      filters: {
        robot_part: ''
      },
      outinfo: [],
      total: 0,
      page: 1,
      sels: [],  //列表选中列
      dialogVisible: false,
      editForm: { // 编辑界面数据
        id: "",
        robot_part: "",
        part_in_num: "",
        part_in_time: "",
        others: "",
      },
      // 验证必填提示
      editFormRules: {
        robot_part: [{ required: true, message: "请输入入库零件名称", trigger: "blur" }],
        part_in_num: [{ required: true, message: "请输入入库零件数量", trigger: "blur" }],

      },
      addFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "新增"
      },
      search: '',

    };
  },

  created () {
    this.get_in_info() ;
  },


  methods: {

        handleSizeChange:function(val) {
            this.currentPage = 1;
            this.pageSize = val;
        },
        handleCurrentChange:function(val) {
            this.currentPage = val;
        },
    get_in_info() {
      // this.loading = true;
      // let para = {
      //   page: this.page,
      // };
      Ininfo().then(resp => {
        this.outinfo = resp.data
        console.log('Outinfo --->', resp.data);
       }).catch(error => {
        console.log(error);
      })
      // this.loading = false;
    },

    // 显示编辑 界面
    handleEdit (index, row) {
      this.dialogVisible = true;
      this.dialogStatus = "update";
      this.editForm = Object.assign({}, row);
    },

    //编辑 确定按钮
    updateData () {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗?", "提示", {}).then(() => {
            let para = Object.assign({}, this.editForm);
            let id = para['id'];
            console.log(para);
            console.log(id);
            para.part_in_time = !para.part_in_time || para.part_in_time == "" ? "" : date.formatDate.format(new Date(para.part_in_time), "yyyy-MM-dd");
            Updtininfo(para,id).then(res => {
              this.$message({
                message: "提交成功",
                type: "success"
              });
              this.$refs["editForm"].resetFields();
              this.dialogVisible = false;
              this.get_in_info();
            })
          }).catch(err => {
            console.log(err)
          })
        }
      })
    },

    // 显示新增 界面
    handleAdd () {
      this.dialogVisible = true;
      this.dialogStatus = "create";
      this.editForm = {

      }
    },
    // 新增 确定按钮
      createData () {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {

            let para = Object.assign({}, this.editForm);
            para.part_in_time = !para.part_in_time || para.part_in_time == "" ? "" : date.formatDate.format(new Date(para.part_in_time), "yyyy-MM-dd");
            console.log(para);
            Addinfo(para).then(res => {
              this.$message({
                message: "提交成功",
                type: "success"
              });
              this.$refs["editForm"].resetFields();
              this.dialogVisible = false;
              this.get_in_info();
            })
          }).catch(err => {
            console.log(e)
          })
        }
      })
    },
    Search() {
      // search 是 v-model="search" 的 search
      var search = this.search;
      if (search) {
        this.outinfo = this.outinfo.filter(function(product) {
          // 每一项数据
          // console.log(product)
          return Object.keys(product).some(function(key) {
            // 每一项数据的参数名
            // console.log(key)
            return (
              String(product[key])
                // toLowerCase() 方法用于把字符串转换为小写。
                .toLowerCase()
                // indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
                .indexOf(search) > -1
            );
          });
        });
      }else{this.get_in_info();}
    },
    goback() {
      this.search='';
      this.get_in_info();
    },
    export_Xls(){
      var a = document.createElement('a');
      var url = 'http://192.168.28.110:9000/api/kucun/export_xls_in';
      var filename = 'data.xlsx';
      a.href=url;
      a.download = filename;
      a.click()
  }

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    // this.get_out_info();
  },

}
</script>

<style scoped>
/* @import url(); 引入公共css类 */
.tab-box {
  background-color: #fff;
  padding: 20px;
}
.toolbar {
  margin-top: 10px;
}

.el-pagination {
    text-align: center; 
}

</style>